// app.js
App({

  onLaunch() {
    //取出数据缓存storage中token，并发送到服务端校验是否有效
    var token = wx.getStorageSync('token');
    this.globalData.token = token;
    //console.log("token:"+this.globalData.token);
    this.getUserid();
  },

  getUserid:function(){
    var token = this.globalData.token;
    var mark = 'getUserid';
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/AppServlet',
      method: 'POST',
      data:{
        mark:mark,
        token:token,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: (res)=>{
        var userid = res.data.data.userid;
        this.globalData.userid = userid;
        //console.log("userid:"+this.globalData.userid);
        this.getUserMoney();  // 由于异步的原因，所以在这里调用，从而获取用户的钱包月
      }
    });
  },

  getUserMoney:function(){
    var userid = this.globalData.userid;
    var mark = 'getUserMoney';
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/AppServlet',
      method: 'POST',
      data:{
        mark:mark,
        userid:userid,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: (res)=>{
        var userMoney = res.data.data.userMoney;
        this.globalData.userMoney = userMoney;
        //console.log("userMoney:"+this.globalData.userMoney);
      }
    });
  },



  // 全局变量
  globalData: {
    token: null,
    userInfo: null,
    getUser:false, // 是否已经获取了用户权限
    credit:0
  }
})